<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="searchForm.orderId"
            placeholder="订单号"
            clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
            v-model="searchForm.posName"
            placeholder="委托人"
            clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getOrderList">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveHandle">创建</el-button>
      </el-form-item>
    </el-form>

    <el-divider><h1>待处理订单</h1></el-divider>
    <br><br>
    <div v-if="this.tableData.length===0">
      <el-empty description="没有更多订单信息咯"></el-empty>
    </div>
    <div v-else>
      <el-table
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          stripe>

        <el-table-column
            prop="orderId"
            label="订单号"
            width="160">
        </el-table-column>
        <el-table-column
            prop="posName"
            label="委托人"
            width="80"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column
            prop="posPhone"
            label="联系电话"
            width="130"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建日期"
            width="200">
          <template slot-scope="scope">
            <el-text>{{scope.row.createDate | dateFilter}}</el-text>
          </template>
        </el-table-column>
        <el-table-column
            prop="price"
            label="快递费用"
            width="80"
            show-overflow-tooltip>
        </el-table-column>

        <el-table-column
            prop="defaults"
            label="当前状态"
            width="120"
        >
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.status===0" type="success">待处理</el-tag>
            <el-tag size="small" v-if="scope.row.status===1" type="success">待揽收</el-tag>
            <el-tag size="small" v-if="scope.row.status===2" type="success">待收货</el-tag>
            <el-tag size="small" v-if="scope.row.status===3" type="success">已签收</el-tag>
            <el-tag size="small" v-if="scope.row.status===4" type="warning">已取消</el-tag>
            <el-tag size="small" v-if="scope.row.status===5" type="danger">已退回</el-tag>
          </template>

        </el-table-column>
        <el-table-column
            prop="icon"
            label="操作">

          <template slot-scope="scope">
            <el-button type="text" @click="editHandle(scope.row.orderId)">添加费用备注</el-button>
            <el-divider direction="vertical"></el-divider>
            <router-link :to="{
              path: '/mew/orderdetail',
              query: {
                orderId: scope.row.orderId
              }
            }">
              <el-button type="text">详情</el-button>
            </router-link>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text"  @click="changeStatus(scope.row.orderId, 1)" >入库</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text"  @click="changeStatus(scope.row.orderId, 5)" >退回</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="current"
          :page-size="size"
          :total="total">
      </el-pagination>
    </div>
    <!--新增对话框-->
    <el-dialog
        :title="operation ==='save' ? '创建订单' : '添加备注' "
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="寄件人姓名" prop="posName" label-width="140px">
          <el-input v-model="editForm.posName" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="寄件人地区" prop="posArea" label-width="140px">
          <el-input v-model="editForm.posArea" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="寄件人地址" prop="posAddress" label-width="140px">
          <el-input v-model="editForm.posAddress" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="寄件人联系方式" prop="posPhone" label-width="140px">
          <el-input v-model="editForm.posPhone" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="posName" label-width="140px">
          <el-input v-model="editForm.porName" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="收件人地区" prop="posName" label-width="140px">
          <el-input v-model="editForm.porArea" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="收件人地址" prop="posName" label-width="140px">
          <el-input v-model="editForm.porAddress" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="收件人联系方式" prop="posName" label-width="140px">
          <el-input v-model="editForm.porPhone" autocomplete="off" :disabled="operation==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="包裹尺寸" prop="size" label-width="140px">
          <el-select v-model="editForm.size" class="filter-item" placeholder="Please select">
            <el-option v-for="item in sizeOption" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="快递费用" prop="price" label-width="140px">
          <el-input v-model="editForm.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="包裹备注" prop="describes" label-width="140px">
          <el-input v-model="editForm.describes" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getStationOrderInfo, getStationOrderList, updateOrSaveStationOrder} from "@/api/sc";
import {change} from "@/api/mew";

export default {
  name: "StationOrder",
  data() {
    return {
      tableData: [],
      total: 0,
      size: 10,
      current: 1,
      dialogVisible: false,
      editForm: {},
      searchForm: {},
      operation: '',
      sizeOption: [
        { key: 0, value: '小件'},
        { key: 1, value: '中件'},
        { key: 2, value: '大件'},
        { key: 3, value: '超大件'},
      ],
      editFormRules: {
        posName: [
          {required: true, message: '请输入寄件人姓名', trigger: 'blur'}
        ],
        posArea: [
          {required: true, message: '请输入寄件人地区', trigger: 'blur'}
        ],
        posAddress: [
          {required: true, message: '请输入寄件人地址', trigger: 'blur'}
        ],
        posPhone: [
          {required: true, message: '请输入寄件人联系方式', trigger: 'blur'}
        ],
        porName: [
          {required: true, message: '请输入收件人姓名', trigger: 'blur'}
        ],
        porArea: [
          {required: true, message: '请输入收件人地区', trigger: 'blur'}
        ],
        porAddress: [
          {required: true, message: '请输入收件人地址', trigger: 'blur'}
        ],
        porPhone: [
          {required: true, message: '请输入收件人联系方式', trigger: 'blur'}
        ],
        size: [
          {required: true, message: '请选择包裹尺寸', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    getOrderList(){
      getStationOrderList(this.searchForm.orderId, this.searchForm.posName).then(res =>{
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.getUserList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getOrderList()
    },
    saveHandle(){
      this.operation = 'save'
      this.dialogVisible = true
    },
    editHandle(id){
      this.operation = 'edit'
      getStationOrderInfo(id).then(res =>{
        this.editForm = res.data.data
        this.dialogVisible = true
      })
    },
    changeStatus(orderId, status){
      change(orderId, status).then(res =>{
        this.$message({
          showClose: true,
          message: res.data.data,
          type: 'success',
          onClose:() => {
            this.getOrderList()
          }
        });
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateOrSaveStationOrder(this.editForm).then(res =>{
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose:() => {
                this.getOrderList()
              }
            });
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>